package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Member;
import com.itheima.pojo.Setmeal;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public Member findByTel(String telephone) {
        return memberMapper.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        memberMapper.add(member);
    }

    @Override
    public List<Integer> getMemberReport(List<String> months) {
        List<Integer> list = new ArrayList<>();
        String beforeMonth;
        String afterMonth;
        for (String month : months) {
            beforeMonth = month + ".31";
            afterMonth = month + ".01";
            //统计在当前月31号之前有多少会员
            Integer countBeforeDate = memberMapper.findMemberCountBeforeDate(beforeMonth);
            //统计在当前月1号之前有多少会员
            Integer countAfterDate = memberMapper.findMemberCountBeforeDate(afterMonth);
            //向统计每个月对应会员数的集合中添加数据
            list.add(countBeforeDate - countAfterDate);
        }
        return list;
    }

    //分页查询获取所有的会员信息返回
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) throws Exception {
        //获取当前页码
        Integer currentPage = queryPageBean.getCurrentPage();
        //获取每页显示条目数
        Integer pageSize = queryPageBean.getPageSize();
        //获取模糊查询的查询条件
        String queryString = queryPageBean.getQueryString();

        //使用分页插件进行分页
        PageHelper.startPage(currentPage, pageSize);

        Page<Member> members = memberMapper.findPage(queryString);

        //获取会员集合
        List<Member> memberList = members.getResult();

        //获取所有条目数
        long total = members.getTotal();
        //封装成结果返回
        return new PageResult(total, memberList);
    }

    //根据会员id查询会员信息
    @Override
    public Member findById(Integer id) {
        return memberMapper.findById(id);
    }

    @Override
    public void edit(Member member) {
        memberMapper.edit(member);

    }

    //添加会员信息
    @Override
    public void addMember(Member member) {

        memberMapper.addMember(member);
    }

    @Override
    //修改客户端个人资料
    public void updateMemberDetail(Member member) {
        memberMapper.updateMemberDetail(member);
    }

    /**
     * 根据id删除会员记录
     *
     * @param id
     * @return void
     * @author JANME
     * @date 2020-09-25 0:33
     */
    @Override
    public boolean deleteMember(Integer id) {

        //删除会员的业务条件
        //1.该会员id无关联的预约订单记录  可以进行删除     否则提示该会员已存在预约记录
        if (orderMapper.findByMemberId(id) > 0) {
            return false;
        } else {
            memberMapper.deleteById(id);
            return true;
        }
    }

   @Override
    //通过日期查询用户的所有预约信息
    public List<Map> findByTelAndOrderDate(Map map) {
        return memberMapper.findByTelAndOrderDate(map);
    }

    @Override
    public List<Map<String, Object>> findAgeByBirthday() {
        return memberMapper.findAgeByBirthday();
    }

    @Override
    public List<Setmeal> findSetmealName(Integer id) {
        return memberMapper.findSetmealByMember(id);
    }

    //根据会员的套餐id查询对应套餐的检查组

    @Override
    public List<CheckGroup> findCheckGroup(Integer id) {
        return memberMapper.findCheckGroupById(id);
    }

    @Override
    //移动端通过手机号查询所有预约记录
    public List<Map> findOrderByTel(String telephone) {
        return memberMapper.findOrderByTel(telephone);
    }

    @Override
    //移动端通过手机号和密码查询登录
    public Member findByTelAndPassword(Map map) {
        return memberMapper.findByTelAndPassword(map);
    }

    @Override//移动端修改密码
    public void editByTel(Member member) {
        memberMapper.edit(member);
    }

    //根据检查组id查询检查项
    @Override
    public List<CheckItem> findCheckItemByCheckGroupId(Integer id) {
        return memberMapper.findCheckItemByCheckGroupId(id);
    }

    @Override
    public void addByCondition(String name, String phoneNumber, Date orderDate) {
        String date = null;
        try {
            date = DateUtils.parseDate2String(orderDate, "yyyy-MM-dd");
        } catch (Exception e) {
            e.printStackTrace();
        }
        memberMapper.addByCondition(name,phoneNumber,date);
    }

    @Override
    public Member findByName(String name) {
        Member member = memberMapper.findByName(name);
        return member;
    }
}
